package com.triones.ljr.controller.api;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.triones.ljr.entity.dto.WxUserDO;
import com.triones.ljr.entity.vo.WxUserVO;
import com.triones.ljr.service.WeChatService;
import com.triones.ljr.service.WxUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * api - 登录、注册
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api")
public class ApiLoginController extends BaseController {

    private final WeChatService weChatService;

    private final WxUserService wxUserService;

    /**
     * 微信登录 返回token
     * @param code
     * @return
     */
    @SaIgnore
    @GetMapping("/login")
    public R<String> wxAuthLogin(@NotBlank(message = "code不能为空") String code) {
        return R.ok("success",weChatService.wxAuthLogin(code));
    }

    /**
     * 获取并绑定手机号
     * @param code
     * @return
     */
    @PostMapping("/bindPhone")
    public R<WxUserDO> bindPhone(@NotBlank(message = "code不能为空")String code) {
        WxUserVO wxUser = wxUserService.getWxUserById();
        String phoneNumber = weChatService.getPhoneNumber(code,wxUser.getOpenid());
        WxUserDO wxUserDO = wxUserService.bindPhone(wxUser, phoneNumber);
        return R.ok("绑定成功",wxUserDO);
    }
}
